package boardgamer.yavalath.client.resources;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import boardgamer.yavalath.R;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Handles dialogs used by multiple activities
 *
 * @author garysoed
 */
public class CommonDialogHandler {
  
  private static CommonDialogHandler INSTANCE = null;
  
  public static void initialize() {
    if (INSTANCE == null) {
      INSTANCE = new CommonDialogHandler();
    }
  }

  public static CommonDialogHandler instance() {
    return checkNotNull(INSTANCE, "CommonDialogHandler not initialized");
  }
  
  private final Intent feedbackChooser;
  private final Intent shareChooser;
  
  private CommonDialogHandler() {
    this.feedbackChooser = createFeedbackIntent();
    this.shareChooser = createShareIntent();
  }

  private Intent createFeedbackIntent() {
    // TODO: Move to strings.xml
    Intent feedbackIntent = new Intent(Intent.ACTION_SENDTO);
    feedbackIntent.setData(Uri.parse("mailto:board.gamer.dev@gmail.com"));
    feedbackIntent.putExtra(Intent.EXTRA_SUBJECT, "Yavalath Feedback");
    return Intent.createChooser(feedbackIntent, "Give feedback via");
  }

  private Intent createShareIntent() {
    // TODO: Move to strings.xml
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Yavalath");
    sharingIntent.putExtra(Intent.EXTRA_TEXT, "Play Yavalath NOW!");
    return Intent.createChooser(sharingIntent, "Give feedback via");
  }

  public Dialog createAboutDialog(final Context context) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
    View aboutScreen = inflater.inflate(R.layout.about, null /* viewRoot */);

    TextView fullNameView = (TextView) aboutScreen.findViewById(R.id.aboutFullName);
    fullNameView.setText(StringHandler.instance().aboutFullName);
    fullNameView.setTextSize(18f);
    fullNameView.setTypeface(ImageHandler.headingFont);

    TextView disclaimerView = (TextView) aboutScreen.findViewById(R.id.aboutDisclaimer);
    disclaimerView.setText(Html.fromHtml(StringHandler.instance().aboutDisclaimerHtml));
    disclaimerView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        context.startActivity(new Intent(Intent.ACTION_VIEW, StringHandler.instance()
            .disclaimerUrl));
      }
    });
    disclaimerView.setTextSize(16f);

    TextView feedbackView = (TextView) aboutScreen.findViewById(R.id.aboutFeedback);
    feedbackView.setText(Html.fromHtml(StringHandler.instance().aboutFeedbackHtml));
    feedbackView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        context.startActivity(feedbackChooser);
      }
    });
    feedbackView.setTextSize(16f);

    TextView releaseNotesView = (TextView) aboutScreen.findViewById(R.id.aboutReleaseNotes);
    releaseNotesView.setText(Html.fromHtml(StringHandler.instance().aboutReleaseNotesHtml));
    releaseNotesView.setTextSize(16f);

    return new AlertDialog.Builder(context)
        .setView(aboutScreen)
        .create();
  }

  public void showError(Context context, String message) {
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
  }

  public void showShareDialog(Context context) {
    context.startActivity(shareChooser);
  }
  
  public void showFeedbackDialog(Context context) {
    context.startActivity(feedbackChooser);
  }
}
